/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.struts.service;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.exception.ServiceException;
import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.service.services.StandardService;
import opiam.admin.faare.service.services.references.ReferenceElement;
import opiam.admin.faare.struts.service.MenusConfig;
import opiam.admin.faare.struts.service.OperatorsConfig;
import opiam.admin.faare.struts.service.StrutsService;
import opiam.admin.faare.struts.service.beans.JBMenu;
import opiam.admin.faare.struts.service.beans.JBMenus;
import opiam.admin.faare.struts.service.beans.JBOperators;
import opiam.admin.faare.struts.service.beans.JBRequests;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Test of the strut service.
 */
public class StrutsServiceTest extends TestCase
{
    /** Search requests sub service configuration file. */
    public static final String REQUESTS_XML_FILE = "/config/service/struts/requests_conf.xml";

    /** Search operators sub service configuration file. */
    public static final String OPERATORS_XML_FILE = "/config/service/struts/operators_conf.xml";

    /** Menus sub service configuration file. */
    public static final String MENUS_XML_FILE = "/config/service/struts/menus_conf.xml";

    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(StrutsServiceTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /** Operator name. */
    private static final String OPERATOR_NAME = "search_sn";

    /** Menu name. */
    private static final String MENU_NAME = "horizontal_menu";

    /** Profile name. */
    private static final String PROFILE_NAME = "profile_par_defaut";

    /** Test user DN. */
    private static final String DN_USER = "uid=jreuter, ou=People, dc=mycompany,dc=com";

    /** Test user login. */
    private static final String LOGIN_USER = "jreuter"; //uid

    /** Test user password. */
    private static final String PASSWORD_USER = "destroy";

    /** User context. */
    private static UserContext _USERCONTEXT = new UserContext();

    /** Instance of JBOperators. */
    private JBOperators pJbOperators = new JBOperators();

    /** Instance of JBMenus. */
    private JBMenus pJbMenus = new JBMenus();

    /** Instance of JBRequests. */
    private JBRequests pJbRequests = new JBRequests();

    /**
    * Creates a new StrutsServiceTest object.
    *
    * @param param  Parameter.
    */
    public StrutsServiceTest(String param)
    {
        super(param);

        try
        {
            PropertiesManager.getInstance();
            StandardService.logon(LOGIN_USER, PASSWORD_USER, _USERCONTEXT);

            StrutsService.initialize(null);
            pJbOperators = OperatorsConfig.getInstance().readConfig(StrutsService.class.getResourceAsStream(
                        OPERATORS_XML_FILE));
            pJbMenus = MenusConfig.getInstance().readConfig(StrutsService.class.getResourceAsStream(
                        MENUS_XML_FILE));
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("StrutsServiceTest constructor error");
            msg.append(TestUtils.NEXT);
            msg.append(e.toString());
            _test.displayResultError(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new StrutsServiceTest("testBegin"));
        suite.addTest(new StrutsServiceTest("testGetOperatorsList"));
        suite.addTest(new StrutsServiceTest("testGetMenu"));
        suite.addTest(new StrutsServiceTest("testGetCurrentProfileMenus"));
        suite.addTest(new StrutsServiceTest("testEnd"));

        return suite;
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("StrutsServiceTest");

        _test.displayHeaderClass("StrutsServiceTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("StrutsServiceTest");
    }

    /**
     * Test the method : getOperatorsList.
     */
    public void testGetOperatorsList()
    {
        _test.displayHeaderMethod("testGetOperatorsList");

        try
        {
            List expectedOperatorsList = new ArrayList();
            List resultOperatorsList = new ArrayList();
            String expectedOperatorsListString = null;
            String resultOperatorsListString = null;

            try
            {
                expectedOperatorsList = pJbOperators.findOperatorByName(OPERATOR_NAME)
                                                    .getRefList();

                resultOperatorsList = StrutsService.getOperatorsList(OPERATOR_NAME);

                StringBuffer buf1 = new StringBuffer();
                java.util.Iterator it1 = null;
                it1 = expectedOperatorsList.iterator();

                ReferenceElement elt1 = null;

                while (it1.hasNext())
                {
                    elt1 = (ReferenceElement) it1.next();
                    buf1.append("   ...");
                    buf1.append(elt1.getLabel());
                    buf1.append(" = ");
                    buf1.append(elt1.getValue());
                }

                expectedOperatorsListString = buf1.toString();

                StringBuffer buf2 = new StringBuffer();
                java.util.Iterator it2 = null;
                it2 = expectedOperatorsList.iterator();

                ReferenceElement elt2 = null;

                while (it2.hasNext())
                {
                    elt2 = (ReferenceElement) it2.next();
                    buf2.append("   ...");
                    buf2.append(elt2.getLabel());
                    buf2.append(" = ");
                    buf2.append(elt2.getValue());
                }

                resultOperatorsListString = buf2.toString();
                _test.compareValues(expectedOperatorsListString,
                    resultOperatorsListString);
            }
            catch (ServiceException e)
            {
                _test.displayResultFailure(e.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetOperatorsList");
    }

    /**
     * Test the method : getMenu.
     *
     */
    public void testGetMenu()
    {
        _test.displayHeaderMethod("testGetMenu");

        try
        {
            JBMenu expectedjbMenu = new JBMenu();
            JBMenu resultjbMenu = new JBMenu();

            expectedjbMenu = pJbMenus.findMenuByName(MENU_NAME,
                    _USERCONTEXT.getJbProfile().getName());
            resultjbMenu = StrutsService.getMenu(MENU_NAME, _USERCONTEXT);
            _test.compareValues(expectedjbMenu.getName(), resultjbMenu.getName());
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetMenu");
    }

    /**
     * Test the method : getCurrentProfileMenus.
     *
     */
    public void testGetCurrentProfileMenus()
    {
        _test.displayHeaderMethod("testGetCurrentProfileMenus");

        try
        {
            Map expectedProfileMenuMap = pJbMenus.getMenusMapByProfile(PROFILE_NAME);
            Map resultProfileMenuMap = StrutsService.getCurrentProfileMenus(_USERCONTEXT);

            if (expectedProfileMenuMap.size() == resultProfileMenuMap.size())
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetCurrentProfileMenus");
    }
}
